package edu.ccut.saturn.basevalidate.businessvalidate;

import java.io.UnsupportedEncodingException;

import edu.ccut.saturn.component.DefaultSaturnValidate;
import edu.ccut.saturn.component.ValidateResult;

public class CheckByteLength extends DefaultSaturnValidate {

	public void doValidate(Object value, Object[] args, ValidateResult result) {

		if (args != null && args.length >= 2) {

			int floor = Integer.parseInt(args[0].toString());
			int top = Integer.parseInt(args[1].toString());
			
			int length = 0;

			try {
				byte[] bytes = value.toString().getBytes("UTF-8");
				length = bytes.length;

			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			if (length < floor || length > top) {
				sendError(_ERROR_, value, result);
			}
		} else {
			sendError(_ARGERROR_, value, result);
		}
	}
}
